package com.erdos.message;

import java.io.Serializable;
import java.util.Date;

import javax.jms.Destination;
import javax.jms.Session;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * encapsulates the sent message header information.
 * @author sunf
 *
 */
public class MessageHeader implements Serializable {
	private static final long serialVersionUID = 2384732736217775250L;
	
	/**
	 * sender id, who sends this message
	 */
	private String sender;
	/**
	 * message type id
	 */
	private String messageTypeId;
	/**
	 * message type name
	 */
	private String messageTypeName;
	
	/**
	 * if this property set to true(default value), it will resolve the property <code>messageTypeId</code><br>
	 * to get the JMS Destination; if set to false, it uses the property <code>receiver</code> as the JNDI name 
	 * to get the JMS Destination. 
	 */
	private boolean resolveReceiverById=true;
	
	/**
	 * jndi of the receiving JMS Destination if resolveReceiverById=false, or receiver name of who receive this message
	 */
	private String receiver;	
	/**
	 * Jms Destination of receiver
	 */
	private Destination destination;
	/**
	 * request-response mode, who will receive the reply, jndi of the JMS Destination
	 */
	private String replyTo;
	/**
	 * Jms Destination of replyTo
	 */
	private Destination replyToDestination;
	
	/**
	 * message length, includes header and body information.
	 * designed for fixed-length message content only.
	 */
	private Integer length;
	
	/**
	 * default is text style, there are still other 2: event and command
	 */
	private MessageType.Type type=MessageType.DOCUMENT();
	
	/**
	 * ascii or utf-8, default is ascii
	 */
	private String charset="ascii"; 
	
	/**
	 * java.lang.Integer, java.lang.String, or anything else of the FQN class type name
	 */
	private String bodyType; 
	/**
	 * should the message be persistent, default is true
	 */
	private boolean persistent=true;
	/**
	 * should the message delivery be under transaction? default is false
	 */
	private boolean transactional;
	/**
	 * the acknowledge mode of the message, default is auto acknowledge mode
	 */
	private int acknowledgeMode=Session.AUTO_ACKNOWLEDGE;
	
	/**
	 * jms correlation id
	 */
	private String jmsCorrelationId;
	/**
	 * correlation message type id
	 */
//	private String correlationMessageTypeId;
//	/**
//	 * referenced message id
//	 */
//	private String correlationMessageId;
	
	private Date sendTime;
	
	private String replyToMessageId;
	
	public MessageHeader() {
		
	}
	public String getSender() {
		return sender;
	}
	public void setSender(String sender) {
		this.sender = sender;
	}
	
	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}
	public Integer getLength() {
		return length;
	}
	public void setLength(Integer length) {
		this.length = length;
	}
	public MessageType.Type getType() {
		return type;
	}
	public void setType(MessageType.Type type) {
		this.type = type;
	}
	public String getCharset() {
		return charset;
	}
	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getReplyTo() {
		return this.replyTo;
	}
	
	public void setReplyTo(String replyTo) {
		this.replyTo = replyTo;
	}
	
	public boolean isPersistent() {
		return persistent;
	}
	public void setPersistent(boolean persistent) {
		this.persistent = persistent;
	}
	public boolean isTransactional() {
		return transactional;
	}
	public void setTransactional(boolean transactional) {
		this.transactional = transactional;
	}
	public int getAcknowledgeMode() {
		return acknowledgeMode;
	}
	public void setAcknowledgeMode(int acknowledgeMode) {
		this.acknowledgeMode = acknowledgeMode;
	}
	
	public String getBodyType() {
		return bodyType;
	}
	
	public void setBodyType(String bodyType) {
		this.bodyType = bodyType;
	}
	
	public String getMessageTypeId() {
		return messageTypeId;
	}
	/**
	 * sets the messageTypeId, this field is required to set.
	 * @param messageId to be set message id
	 */
	public void setMessageTypeId(String messageTypeId) {
		this.messageTypeId = messageTypeId;		
	}
	/**
	 * whether resolve the JMS Destination by receiver or messageTypeId.
	 * @return true if resolve by receiver's Jndi name, false if resolve by messageTypeId
	 */
	public boolean isResolveReceiverById() {
		return resolveReceiverById;
	}
	
	/**
	 * @return the jmsCorrelationId
	 */
	public String getJmsCorrelationId() {
		return jmsCorrelationId;
	}
	/**
	 * @param jmsCorrelationId the jmsCorrelationId to set
	 */
	public void setJmsCorrelationId(String jmsCorrelationId) {
		this.jmsCorrelationId = jmsCorrelationId;
	}	
	
	/**
	 * @return the messageTypeName
	 */
	public String getMessageTypeName() {
		return messageTypeName;
	}
	/**
	 * @param messageTypeName the messageTypeName to set
	 */
	public void setMessageTypeName(String messageTypeName) {
		this.messageTypeName = messageTypeName;
	}
	/**
	 * @return the correlationMessageTypeId
	 */
//	public String getCorrelationMessageTypeId() {
//		return correlationMessageTypeId;
//	}
//	/**
//	 * @param correlationMessageTypeId the correlationMessageTypeId to set
//	 */
//	public void setCorrelationMessageTypeId(String correlationMessageTypeId) {
//		this.correlationMessageTypeId = correlationMessageTypeId;
//	}
//	/**
//	 * @return the correlationMessageId
//	 */
//	public String getCorrelationMessageId() {
//		return correlationMessageId;
//	}
//	
//	/**
//	 * @param correlationMessageId the correlationMessageId to set
//	 */
//	public void setCorrelationMessageId(String correlationMessageId) {
//		this.correlationMessageId = correlationMessageId;
//	}	
	
	/**
	 * @return the sendTime
	 */
	public Date getSendTime() {
		return sendTime==null?new Date():this.sendTime;
	}
	
	/**
	 * @return the replyToMessageId
	 */
	public String getReplyToMessageId() {
		return replyToMessageId;
	}
	/**
	 * @param replyToMessageId the replyToMessageId to set
	 */
	public void setReplyToMessageId(String replyToMessageId) {
		this.replyToMessageId = replyToMessageId;
	}
	/**
	 * @param sendTime the sendTime to set
	 */
	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}	
	
	/**
	 * @return the destination
	 */
	public Destination getDestination() {
		return destination;
	}
	/**
	 * @param destination the destination to set
	 */
	public void setDestination(Destination destination) {
		this.destination = destination;
	}
	/**
	 * @return the replyToDestination
	 */
	public Destination getReplyToDestination() {
		return replyToDestination;
	}
	/**
	 * @param replyToDestination the replyToDestination to set
	 */
	public void setReplyToDestination(Destination replyToDestination) {
		this.replyToDestination = replyToDestination;
	}
	@Override
	public String toString() {		
		return new ToStringBuilder(this).append(this.messageTypeId).append(this.sender)
			.append(this.receiver).append(this.replyTo).append(this.type).append(this.length)
			.append(this.charset).append(this.bodyType).toString();
	}
	
}
